<?php

namespace App\Http\Controllers\commodity;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\main\MainController as main;
use DB;

class InventoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        $goods_id = $_GET['id'];
        $goods_model = $_GET['goods_model'];
        return view('admin/inventory/add',['config'=>main::GoodsConfigCate(),'goods_id'=>$goods_id,'goods_model'=>$goods_model]);
    }


    /**
     * postInsert()   向数据库中添加商品子类配置
     * 
     * @return        如果添加成功则返回商品视图
     */
    public function postInsert(Request $request)
    {
        // dd($request -> all());
    if($request -> hasFile('pic1')){
            

            // 获取文件后缀名
            $suffix = '.'.$request -> file('pic1') ->getClientOriginalExtension();
            // 随机文件名
            $picname1 = time().rand(1000,1000000).$suffix;
           
            
            // 将上传照片移动到指定文件夹中
            $request->file('pic1')->move('./images/admin/goods_pic', $picname1);

           
            
        }else{
            return back() -> withInput() -> with('error','请上传商品照片');
        }
        // 将商品图片名称写入数据库中
        $goods_datalist_pic = DB::table('goods_datalist_pic') -> insertGetId(['goods_datalist_pic1'=>$picname1]);
        if(!$goods_datalist_pic && $goods_datalist_pic > 0) {
            return back() -> withInput() -> with('error','图片写入数据库失败');
        }
        // 获取子类商品配置
        // 接收配置详情
        $sele = $request -> only('sele')['sele'];

        // dd($sele);
        // 遍历接收的id
        foreach($sele as $key=>$value) {
            // 如果接收到的值为1的话,停止一次
            if($value == 1) {
                continue;
            }
            // 将过滤以后的值放入数组中
            $id[] = $value;
        }
        // 查询数据库
        $data = DB::table('goods_config_cate') 
        // 查询条件为数据库中的主键id等于接收的id
        -> whereIn('id',$id) 
        // 过滤父级id为0的数据
        -> where('goods_config_sort','!=',0) 
        // 去除配置名称和配置字段名称
        -> select('goods_config_cate','goods_config_name') 
        -> get();


        // 遍历数据库中查询到的数据
        foreach($data as $key=>$value) {
            // 将查询到的配置名称字段添加到数组中
            $goods_config_cate[] = $value['goods_config_cate'];
            // 将查询到的配置id的数据库字段名称添加到数组中
            $goods_config_name[] = $value['goods_config_name'];
        }
        // 合并数组
        $goods_config = array_combine($goods_config_name,$goods_config_cate);
        // dd($goods_config);
        // 将商品配置添加到数据库,并获取到插入的id
        $goods_config_id = DB::table('goods_config_list') ->insertGetId($goods_config);
        // 判断如果商品配置没有插入成功的话的则返回到添加页面
        if(!$goods_config_id && $goods_config_id > 0) {
            return back() -> withInput() -> with('error','添加商品配置失败,请检查商品配置是否正确');
        }
        
         // 获取商品其他配置信息,
        $goods_config_other = $request -> only('goods_config_other_system','goods_config_other_camera_f','goods_config_other_camera_b','goods_config_other_finger','goods_config_other_battery');
        // 将商品其他配置信息插入数据库中
        $goods_config_other = DB::table('goods_config_other') -> insertGetId($goods_config_other);
        if(!$goods_config_other) {
            return back() -> withInput() -> with('error','商品其他配置信息插入数据库失败,请检查商品其他配置信息是否填写');
        } 
        // 获取商品id
        $goods_id = $_POST['goods_id'];
        // dd($request -> all());
        // 接收商品价格
        $goods_price = $_POST['goods_price'];
        // 将自类商品配置信息写入配置表中
        $goods_config = array(
                'goods_id' => $goods_id,
                'goods_config_id' => $goods_config_id,
                'goods_config_other' => $goods_config_other,
                'goods_price' => $goods_price,
                'goods_datalist_pic' => $goods_datalist_pic,
                'goods_config_status' => 1

            );
        // 写入数据库
        $goods_config_id = DB::table('goods_config') -> insertGetId($goods_config);
    
        if(!$goods_config_id && $goods_config_id > 0) {
            return back() -> withInput() -> with('error','商品配置添加失败');
        }

        // 获取商品颜色,商品库存
        $data = $request -> only('goods_color','goods_inventory');
        // 将新插入配置id压入到数组中
        $data['goods_config'] = $goods_config_id;
        // 写入商品库存表
        $res = DB::table('goods_inventory') -> insert($data);
        if($res) {
            return redirect('admin/commodity/list') -> with('success','商品子类配置添加成功');
        }

    }

    /**
     * 
     */
    public function getGoodsson()
    {
        // 获取商品id
        $id = $_GET['id'];
        // 关联查询商品的子类配置
        $data = DB::table('goods_config')
        -> join('goods','goods.id','=','goods_config.goods_id')
        -> join('goods_inventory','goods_inventory.goods_config','=','goods_config.id')
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        -> where('goods_config.goods_config_status','=','1')
        -> where('goods.id','=',$id)
        -> get();
        echo json_encode($data);
    }
    
}
